package client

import (
	"fmt"

	. "starsine.cn/appTpl/cmn/defs"
	"starsine.cn/appTpl/cmn/logs"
	"starsine.cn/appTpl/svclog/peers/svclogproto"
	"starsine.cn/appTpl/svcpeers"
	"starsine.cn/appTpl/svcregistry"
)

var svcDepStatus *SvcDepStatus = nil

func Start(appCtxt *AppContext) {
	svcpeers.Start(appCtxt)

	svcDepStatus = CreateSvcDepStatus(appCtxt)
	svcDepStatus.Start()

	logs.Infof("peers.client start")
}

func DumpSvcDepStatus() string {
	return svcDepStatus.String()
}

func SvcLogWrite(v []byte) (int, error) {
	r, err := svcDepStatus.svcDeps.SvcLogWriteLog(&svclogproto.WriteLogReq{
		InstanceID: svcregistry.GetInstanceID(),
		Data:       v,
		Len:        uint32(len(v)),
	})

	if err != nil {
		return 0, err
	}
	if r.Succ {
		return len(v), nil
	} else {
		return 0, fmt.Errorf("SvcLogWrite 0 byte.")
	}
}
